<template>
  <div class="login">
    <div class="loginBox">
      <h2>管理员登陆</h2>
      <div class="item">
        <input type="text" required v-model="username" maxlength="11" />
        <p>用户名</p>
      </div>
      <div class="item">
        <input type="password" required v-model="password" />
        <p>密码</p>
      </div>
      <div class="jzm_text">
        <div class="item jzm">
          <input type="text" required v-model="jym" />
          <p>校验码</p>
        </div>
        <button class="getJzm" @click="getJym">获取校验码</button>
      </div>

      <button class="btn" @click="login">
        登录
        <span></span>
        <span></span>
        <span></span>
        <span></span>
      </button>
    </div>
  </div>
</template>

<script>
import md5 from "md5";
import sha256 from "crypto-js/sha256";
import moment from "moment";
export default {
  name: "login",
  data() {
    return {
      username: "",
      password: "",
      jym: "",
      scJzm: "",
    };
  },
  components: {},
  computed: {
    // codeval: {
    //   get: function () {
    //     return this.username;
    //   },
    //   set: function (val) {
    //     this.username = val.toUpperCase();
    //   },
    // },
  },
  //   mounted(){},
  methods: {
    onSubmit() {
      return false;
    },
    login() {
      if (this.jym === "") {
        this.$message({
          message: "请输入校验码",
          type: "warning",
        });
      } else if (this.jym !== this.scJzm) {
        this.$message({
          message: "校验码错误",
          type: "error",
        });
      } else if (this.username == "") {
        this.$message({
          message: "请输入用户名",
          type: "warning",
        });
      } else {
        this.$axios({
          method: "POST",
          url: "/api/Login",
          data: {
            UAdminPhone: this.username,
            UAdminPwd: md5(sha256(this.password).words),
            // console.log(md5(sha256(this.UAdminPwd).words));
            type_: "pc",
          },
        }).then((req, res) => {
          // console.log(req.data);
          // return;
          switch (req.data.status) {
            case -1: {
              this.$message({
                message: "用户不存在!",
                type: "warning",
              });
              break;
            }
            // case -2: {
            //   this.$message({
            //     message: "用户不存在!",
            //     type: "primary",
            //   });
            //   break;
            // }
            case -4: {
              this.$message({
                message: "密码错误!",
                type: "error",
              });
              break;
            }
            case 1: {
              let endTime = new Date(req.data.data[0].EndTime).getTime();
              let thisTime = new Date(moment().format("L")).getTime();
              // console.log(endTime, thisTime)
              if (endTime >= thisTime) {
                this.$message({
                  message: "登陆成功!",
                  type: "success",
                });
                let code = {
                  _id: req.data.data[0]._id,
                  UAdminPhone: req.data.data[0].UAdminPhone,
                  StoreName: req.data.data[0].StoreName,
                  EStoreName: req.data.data[0].EStoreName,
                  jym: md5(this.jym),
                  sign_id: req.data.data[0].sign_id,
                };
                // console.log(req.data.data[0])
                // this.$store.AdminInfo = req.data.data[0];
                let param = JSON.stringify(code);
                localStorage.isLogin = param;
                // localStorage.isAdmin = false;
                localStorage.isLogin_ = true;
                localStorage.page = 1;
                // document.cookie = "code=" + md5(this.code);
                // unit.setCookie("code", md5(this.code), 86400);
                // unit.setCookie("phone", this.UAdminPhone, 86400);
                // unit.setCookie("isAdmin", false);
                setTimeout(() => {
                  this.$router.push({ path: "/index", replace: true });
                }, 2000);
              } else {
                this.$message({
                  message: "当前账号已到期！到期时间:" + req.data.data[0].EndTime,
                  type: "error",
                });
                return
              }
              break;

              // let user = {
              //   UAdminPhone: md5(this.username),
              //   phone: this.username,
              //   StoreName: req.data.data,
              //   code: md5(this.jym),
              // };
              // localStorage.isLogin_ = true;
              // let param = JSON.stringify(user);
              // localStorage.isLogin = param;
              // localStorage.isAdmin = false;

              // document.cookie = "code=" + md5(this.code);
              // unit.setCookie("code", md5(this.code), 86400);
              // unit.setCookie("phone", this.UAdminPhone, 86400);
              // unit.setCookie("isAdmin", false);
              // setTimeout(() => {
              //   // this.$router.push("/user");
              //   const target = this.$route.query.target;
              //   this.$router.push({ path: "/index", replace: true });
              // }, 1000);

            }
          }
        });
        // this.$axios.get("/api/config").then((res) => {
        //   if (res.data.status == -1) {
        //     this.$message({
        //       message: "配置文件丢失!",
        //       type: "error",
        //     });
        //   } else {
        //     let pass = res.data.data.password[0];
        //     if (md5(this.username) != pass) {
        //       this.$message({
        //         message: "用户名错误！",
        //         type: "error",
        //       });
        //     } else {
        //       this.$message({
        //         message: "登陆成功！",
        //         type: "success",
        //       });
        //       localStorage.isLogin = true;
        //       localStorage.username = md5(this.username);
        //       localStorage.page = 1;
        //       setTimeout(() => {
        //         const target = this.$route.query.target;
        //         this.$router.push({ path: "/index", replace: true });
        //       }, 1000);
        //     }
        //   }
        // });
      }
    },
    getJym() {
      this.scJzm = "";
      const arr =
        "qwertyuipasdfghjklzxcvbnm123456789QWERTYUIOPASDFGHJKLZXCVBNM";
      for (let i = 0; i < 4; i++) {
        const text = Math.round(Math.random() * arr.length);
        this.scJzm += arr[text];
      }
      //   console.log(this.scJzm);
      this.jym = this.scJzm;
    },
  },
};
</script>
<style scoped>
@import "../static/login.css";
</style>
